﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.DAL;
using Core.Utils;

namespace Core.BOL
{
    public class LinkRepository
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public LinkRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public void InsertLink(tSite_Link link)
        {
            context.tSite_Links.InsertOnSubmit(link);
        }

        public List<tSite_Link> GetLinks(int skip, int take, bool validation)
        {
            var query = (from l in context.tSite_Links
                         orderby l.Validation ascending, l.Title ascending
                         select l);
            if (validation)
            {
                return query.Where(p => p.Validation == validation).Skip(skip).Take(take).ToList();
            }
            return query.Skip(skip).Take(take).ToList();
        }

        public tSite_Link GetLinkByID(int ID)
        {
            return context.tSite_Links.Where(p => p.ID == ID).SingleOrDefault();
        }

        public int GetCountLinks(bool validation)
        {
            var query = (from n in context.tSite_Links
                         select n);

            if (validation)
            {
                return query.Where(p => p.Validation == validation).Count();
            }

            return query.Count();
        }

        public void ValidateLink(int ID)
        {
            var link = context.tSite_Links.Where(p => p.ID == ID).SingleOrDefault();
            if (link != null)
            {
                link.Validation = true;
            }
        }

        public void DeleteLink(int ID)
        {
            var link = context.tSite_Links.Where(p => p.ID == ID).SingleOrDefault();
            if (link != null)
            {
                context.tSite_Links.DeleteOnSubmit(link);
            }
        }

        public void Save()
        {
            context.SubmitChanges();
        }
        #endregion
    }
}
